package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.record.DVALRecord;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public TurnoverReportVO getTurnoverReport(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();
        list.add(begin);
        list.add(end);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            list.add(begin);
        }
        String join = StringUtils.join(list, ",");
        List<Double> turnnoverList = new ArrayList<>();
        for (LocalDate date : list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status", Orders.COMPLETED);
            Double turnnover = reportMapper.sumByMap(map);
            if (turnnover == null) {
                turnnover = 0.0;
            }
            turnnoverList.add(turnnover);
        }
        return TurnoverReportVO
                .builder()
                .turnoverList(StringUtils.join(turnnoverList, ","))
                .dateList(join)
                .build();
    }

    @Override
    public UserReportVO getUserReport(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();
        list.add(begin);
        list.add(end);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            list.add(begin);
        }
        String join = StringUtils.join(list, ",");

        List<Integer> newUserlist = new ArrayList<>();
        List<Integer> totalUserlist = new ArrayList<>();

        for (LocalDate date : list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map newUserMap = new HashMap<>();
            Map totalUserMap = new HashMap<>();

            newUserMap.put("begin", beginTime);
            newUserMap.put("end", endTime);

            totalUserMap.put("end", endTime);
            Integer countUser = userMapper.countByMap(totalUserMap);
            Integer newCountUser = userMapper.countByMap(newUserMap);
            totalUserlist.add(countUser);
            newUserlist.add(newCountUser);
        }

        return UserReportVO.builder()
                .dateList(join)
                .totalUserList(StringUtils.join(totalUserlist, ","))
                .newUserList(StringUtils.join(newUserlist, ",")).build();
    }

    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();
        list.add(begin);
        list.add(end);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            list.add(begin);
        }
        String join = StringUtils.join(list, ",");
        //订单数列表
        List<Integer> ordersList = new ArrayList<>();
        //有效订单数列表
        List<Integer> ordersEffectiveList = new ArrayList<>();
        //一天
        for (LocalDate date : list) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map ordersMap = new HashMap<>();//总数
            Map ordersEffectiveMap = new HashMap<>();//有效

            //有效
            ordersEffectiveMap.put("beginTime", beginTime);
            ordersEffectiveMap.put("endTime", endTime);
            ordersEffectiveMap.put("status", Orders.COMPLETED);
            Integer ordersEffective = orderMapper.countOrders(ordersEffectiveMap);
            ordersEffectiveList.add(ordersEffective);
            //total
            ordersMap.put("beginTime", beginTime);
            ordersMap.put("endTime", endTime);
            Integer orders = orderMapper.countOrders(ordersMap);
            ordersList.add(orders);
        }
        Integer orders = ordersList.stream().reduce(Integer::sum).get();
        Integer ordersEffective = ordersEffectiveList.stream().reduce(Integer::sum).get();
        Double totalCompletionRate = 0.0;
        if (orders != 0) {

            totalCompletionRate = ordersEffective.doubleValue() / orders;
        }
        return OrderReportVO.builder()
                .dateList(join)
                .orderCompletionRate(totalCompletionRate)
                .orderCountList(StringUtils.join(ordersList, ","))
                .validOrderCountList(StringUtils.join(ordersEffectiveList, ","))
                .totalOrderCount(orders)
                .validOrderCount(ordersEffective)
                .build();
    }

    @Override
    public SalesTop10ReportVO salesTop(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        List<GoodsSalesDTO> salesTop = orderMapper.getSalesTop(beginTime, endTime);

        List<String> nameList = salesTop.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String name = StringUtils.join(nameList, ",");

        List<Integer> numberList = salesTop.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String number = StringUtils.join(numberList, ",");

        return SalesTop10ReportVO.builder()
                .nameList(name)
                .numberList(number)
                .build();
    }
}
